package com.cms.recursoshumanos.component;

import com.cms.recursoshumanos.component.base.LabelListMenuItem;
import com.cms.recursoshumanos.component.base.ListStyleButtonField;
import com.cms.recursoshumanos.object.ArticuloCarrito;

import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Characters;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.DrawStyle;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.Touchscreen;
import net.rim.device.api.ui.Ui;

public class ArticuloCostumListElement extends ListStyleButtonField 
{

  private static final int WIDTH = Display.getWidth()/2;
  public static int DRAWPOSITION_TOP = 0;
  public static int DRAWPOSITION_BOTTOM = 1;
  public static int DRAWPOSITION_MIDDLE = 2;
  public static int DRAWPOSITION_SINGLE = 3;
  public static int SEPARATOR_LONG = 20;
  public static int DO_ACTION = 999;
  public static int NO_ACTION = -999;
  
  protected static final int CORNER_RADIUS = 18;
  
  private static final int HPADDING = (WIDTH <= 320) ? 6 : 8;
  private static final int VPADDING = 2;
  
  private static final int COLOR_BACKGROUND = 0xFFFFFF;
  protected static final int COLOR_BORDER = 0xBBBBBB;
  protected static int COLOR_BACKGROUND_FOCUS = 0xE4E4E4;
  protected static int COLOR_BACKGROUND_FOCUS_ACTION = 0x186DEF;
  
  //private LabelListMenuItem _labelField;
  //private LabelListMenuItem _contentField;
  private LabelListMenuItem _articuloField;
  private LabelListMenuItem _cantidadField;
  private LabelListMenuItem _precioField;
  //private LabelListMenuItem _contentField;
 
  
  private ArticuloCarrito elArticulo;
  
  private int _targetHeight;
  private int _rightOffset;
  private int _leftOffset;
  private int _articuloHeight;
  private int _articuloWidth;
  //private int _labelHeight;
  //private int _labelWidth;
  //private int _contentHeight;
  //private int _contentWidth;
  private int _cantidadHeight;
  private int _cantidadWidth;
  private int _precioHeight;
  private int _precioWidth;
  
  protected int _preferedWidth;
  protected boolean _doAction = false;
  
  protected int _drawPosition = -1;

  public ArticuloCostumListElement( ArticuloCarrito elArticuloCarrito, long style ) 
  {
      this( elArticuloCarrito, null, style );
  }
    
  public ArticuloCostumListElement( ArticuloCarrito elArticuloCarrito, int width ) 
  {
      this( null, elArticuloCarrito, null, 0, width );
  }
  
  public ArticuloCostumListElement( ArticuloCarrito elArticuloCarrito, int type, int width ) 
  {
      this( null, elArticuloCarrito, null, 0, width );
      if(type == DO_ACTION) 
    	  _doAction = true;
  }
  
  public ArticuloCostumListElement( ArticuloCarrito elArticuloCarrito, int type, int width, Bitmap actionIcon ) 
  {
      this( null, elArticuloCarrito, actionIcon, 0, width );
      if(type == DO_ACTION) 
    	  _doAction = true;
  }
  
  public ArticuloCostumListElement( ArticuloCarrito elArticuloCarrito, Bitmap actionIcon ) 
  {
      this( elArticuloCarrito, actionIcon, 0 );
  }
  
  public ArticuloCostumListElement( ArticuloCarrito elArticuloCarrito, Bitmap actionIcon, long style ) 
  {
      this( null, elArticuloCarrito, actionIcon, style, 0 );
  }
  
  public ArticuloCostumListElement( Bitmap icon, ArticuloCarrito elArticuloCarrito, long style ) 
  {
      this( icon, elArticuloCarrito, null, style, 0 );
  }
  
  public void setType(int type) {
	  if(type == DO_ACTION) 
    	  _doAction = true;
	  else
		  _doAction = false;
  }
  
  public ArticuloCostumListElement( Bitmap icon, ArticuloCarrito elArticuloCarrito, 
		  Bitmap actionIcon, long style, int width ) 
  {
	  super( Field.FOCUSABLE );
	  String articulo = elArticuloCarrito.getArticulo().getNombreArticulo();
	  int cantidad = elArticuloCarrito.getCantidadSeleccionada();
	  float precio = elArticuloCarrito.getArticulo().getPrecioUnitario();
	  Font defaultFont = getFont();
      Font font = defaultFont.derive(Font.PLAIN, 6, Ui.UNITS_pt);       
      _articuloField = new LabelListMenuItem( articulo, style );
      _cantidadField = new LabelListMenuItem( ""+cantidad, style );
      _precioField = new LabelListMenuItem( ""+precio*cantidad, style );
      _articuloField.setFont(font);
      _cantidadField.setFont(font);
      _precioField.setFont(font);
      
      //_actionIcon = actionIcon;
      //_leftIcon = icon;
      _preferedWidth = width;
  }
  
  public ArticuloCostumListElement(long l) {
	  super(l);
  }
  
  public String getArticulo() {
	  return this._articuloField.getText();
  }
  public String getCantidad() {
	  return this._cantidadField.getText();
  }
  public String getPrecio() {
	  return this._precioField.getText();
  }


  public void setDrawPosition( int drawPosition ) {
      _drawPosition = drawPosition;
  }
  
  public String articulotoString() {
      return _articuloField.toString();
  }
  public String cantidadtoString() {
      return _cantidadField.toString();
  }
  public String preciotoString() {
      return _precioField.toString();
  }
  
  public void layout( int width, int height ) {

      _targetHeight = getFont().getHeight() / 2 * 3 + 2 * VPADDING;
//#ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
      if( Touchscreen.isSupported() ) {
          _targetHeight = getFont().getHeight() * 2 + 2 * VPADDING;
      }
//#endif
  	
      _leftOffset = HPADDING;
      /*if( _leftIcon != null ) {
          _leftOffset += _leftIcon.getWidth() + HPADDING;
      }*/
      
      _rightOffset = HPADDING;
      if( _precioField != null ) {
          _rightOffset += _precioField.getWidth() + HPADDING;
      }
      
      _articuloWidth = _preferedWidth / 4;
      _cantidadWidth = getFont().getAdvance(_cantidadField.getText());
      _precioWidth = getFont().getAdvance(_precioField.getText());
      
      /*int articuloWidth = _preferedWidth -
    		  _articuloWidth - SEPARATOR_LONG;
      int height1 = height;
      int tamArticulo = getFont().getHeight();
      
      
      if(articuloWidth < _articuloWidth) {
    	  height1 = (_articuloWidth%articuloWidth)*(tamArticulo+2);
      }*/
     
      
      int cantidadWidth = _preferedWidth - _leftOffset - _rightOffset - 
    		  _cantidadWidth - SEPARATOR_LONG;
      int height2 = height;
      int tamCantidad = getFont().getHeight();
      
      
      if(cantidadWidth < _cantidadWidth) {
    	  height2 = (_cantidadWidth%cantidadWidth)*(tamCantidad+2);
      }
      
      
      
      int precioWidth = _preferedWidth - _leftOffset - _rightOffset - 
    		  _articuloWidth - _cantidadWidth  - SEPARATOR_LONG;
      int height3 = height;
      int tamPrecio = getFont().getHeight();
      
      
      if(precioWidth < _precioWidth) {
    	  height3 = (_precioWidth%precioWidth)*(tamPrecio+2);
      }
      
      _articuloField.layout( _preferedWidth - _cantidadWidth - _precioWidth, height );
      _articuloHeight = _articuloField.getHeight();
      _cantidadField.layout( _preferedWidth - _articuloWidth - _precioWidth, height2 );
      _cantidadHeight = _cantidadField.getHeight();
      _precioField.layout( _preferedWidth - _articuloWidth - _cantidadWidth, height3 );
      _precioHeight = _precioField.getHeight();
      
      
      if( _articuloField.isStyle( DrawStyle.HCENTER ) ) {
          _leftOffset = ( width - _cantidadWidth - _precioWidth ) / 2;
      } else if ( _articuloField.isStyle( DrawStyle.RIGHT ) ) {
          _leftOffset = width - _cantidadWidth - _precioWidth - HPADDING - _rightOffset;
      }
      
      if( _cantidadField.isStyle( DrawStyle.HCENTER ) ) {
          _leftOffset = ( width - _articuloWidth - precioWidth ) / 2;
      } else if ( _cantidadField.isStyle( DrawStyle.RIGHT ) ) {
          _leftOffset = width - _articuloWidth - precioWidth - HPADDING - _rightOffset;
      }
      
      if( _precioField.isStyle( DrawStyle.HCENTER ) ) {
          _leftOffset = ( width - _articuloWidth - cantidadWidth ) / 2;
      } else if ( _precioField.isStyle( DrawStyle.RIGHT ) ) {
          _leftOffset = width - _articuloWidth - cantidadWidth - HPADDING - _rightOffset;
      }

      int extraVPaddingNeeded = 0;
      if( Math.max(_articuloHeight, _precioHeight ) < _targetHeight ) {
          // Make sure that they are at least 1.5 times font height
          extraVPaddingNeeded =  ( _targetHeight - Math.max(_articuloHeight, _precioHeight)  ) / 2;
      }
      
      setExtent( width, Math.max(_articuloHeight,_precioHeight));
  }
  
  public void updateArticulo( String text ) 
  {
	  _articuloField.setText(text);
	  updateLayout();
  }
  public void updateCantidad( String text ) 
  {
	  _cantidadField.setText(text);
	  updateLayout();
  }
  public void updatePrecio( String text ) 
  {
	  _precioField.setText(text);
	  updateLayout();
  }
  
  protected void paint( Graphics g ) {
      /*// Left Bitmap
      if( _leftIcon != null ) {
          g.drawBitmap( HPADDING, VPADDING, _leftIcon.getWidth(), _leftIcon.getHeight(), 
        		  _leftIcon, 0, 0 );
      }*/
      
      // Text 
      // articulofield
      try {
          g.pushRegion( HPADDING, VPADDING, _articuloField.getWidth(), 
        		  _articuloField.getHeight(), 0, 0 );
          _articuloField.paint( g );
      } finally {
          g.popContext();
      }
      //cantidadField
      try {
          g.pushRegion( _leftOffset+ _articuloWidth+SEPARATOR_LONG, 
        		  ( getHeight() - _cantidadHeight ) / 2, 
        		  getWidth() - _leftOffset - _rightOffset - _articuloWidth, 
        		  _cantidadHeight, 0, 0 );
          _cantidadField.paint( g );
      } finally {
          g.popContext();
      }
      //precioField
      try {
          g.pushRegion( _leftOffset+_articuloWidth+_cantidadWidth+SEPARATOR_LONG+SEPARATOR_LONG, 
        		  ( getHeight() - _precioHeight ) / 2, 
        		  _precioField.getWidth(), 
        		  _precioField.getHeight(), 0, 0 );
          _precioField.paint( g );
      } finally {
          g.popContext();
      }
      
      // Right (Action) Bitmap
      /*if( _precioField != null ) {
          g.drawBitmap( getWidth() - HPADDING - _precioField.getWidth(), ( getHeight() - 
        		  _precioField.getHeight() ) / 2, _precioField.getWidth(), _precioField.getHeight(), 
        		  _actionIcon, 0, 0 );
      }*/
  }
  
  protected void paintBackground( Graphics g ) {
      if( _drawPosition < 0 ) {
          // it's like a list field, let the default background be drawn
          super.paintBackground( g );
          return;
      }
      
      int oldColour = g.getColor();
      int focus = COLOR_BACKGROUND_FOCUS;
      if(_doAction) 
    	  focus = COLOR_BACKGROUND_FOCUS_ACTION;
      
      int background = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS ) ? focus : COLOR_BACKGROUND;
      
      try {
    	  
          if( _drawPosition == 0 ) {
              // Top
              g.setColor( background );
              g.fillRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
              g.setColor( COLOR_BORDER );
              g.drawRoundRect( 0, 0, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
          } else if( _drawPosition == 1 ) {
              // Bottom 
              g.setColor( background );
              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
              g.setColor( COLOR_BORDER );
              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
          } else if( _drawPosition == 2 ) {
              // Middle
              g.setColor( background );
              g.fillRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
              g.setColor( COLOR_BORDER );
              g.drawRoundRect( 0, -CORNER_RADIUS, getWidth(), getHeight() + 2 * CORNER_RADIUS, CORNER_RADIUS, CORNER_RADIUS );
              g.drawLine( 0, getHeight() - 1, getWidth(), getHeight() - 1 );
          } else {
              // Single
              g.setColor( background );
              g.fillRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
              g.setColor( COLOR_BORDER );
              g.drawRoundRect( 0, 0, getWidth(), getHeight(), CORNER_RADIUS, CORNER_RADIUS );
          }
          g.drawLine(_preferedWidth+SEPARATOR_LONG, 0, _preferedWidth+SEPARATOR_LONG, getHeight());
      } finally {
          g.setColor( oldColour );
      }
  }
  
  protected void drawFocus( Graphics g, boolean on ) {
      if( _drawPosition < 0 ) {
          super.drawFocus( g, on );
      } else {
          boolean oldDrawStyleFocus = g.isDrawingStyleSet( Graphics.DRAWSTYLE_FOCUS );
          try {
              if( on ) {
            	  g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, true );
              }
              paintBackground( g );
              paint( g );
          } finally {
              g.setDrawingStyle( Graphics.DRAWSTYLE_FOCUS, oldDrawStyleFocus );
          }
      }
  }
  
  protected boolean keyChar( char character, int status, int time ) {
      if( character == Characters.ENTER ) {
          clickButton();
          return true;
      }
      return super.keyChar( character, status, time );
  }
  
  protected boolean navigationClick( int status, int time ) {
      clickButton(); 
      return true;    
  }
  
  protected boolean trackwheelClick( int status, int time ) {        
      clickButton();    
      return true;
  }
  
  //#ifndef VER_4.1.0 | 4.0.0
  protected boolean invokeAction( int action ) {
	  switch( action ) {
	      case ACTION_INVOKE: {
	          clickButton(); 
	          return true;
	      }
	  }
	  return super.invokeAction( action );
  }
  //#endif        
       
  /**
   * A public way to click this button
   */
  public void clickButton() {
      fieldChangeNotify( 0 );
  }
     
//#ifndef VER_4.6.1 | VER_4.6.0 | VER_4.5.0 | VER_4.2.1 | VER_4.2.0
  protected boolean touchEvent( TouchEvent message ) {
      int x = message.getX( 1 );
      int y = message.getY( 1 );
      if( x < 0 || y < 0 || x > getExtent().width || y > getExtent().height ) {
          // Outside the field
          return false;
      }
      switch( message.getEvent() ) {
          case TouchEvent.UNCLICK:
              clickButton();
              return true;
      }
      return super.touchEvent( message );
  }
//#endif 

  public void setDirty( boolean dirty ) {}
  public void setMuddy( boolean muddy ) {}
  
 
}